﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Base.cshtml"; 
}
@section head{
    <script src="@Url.Content("~/Content/roohui/jquery-validation/jquery.validate.min.js")"></script>
    <script src="@Url.Content("~/Content/roohui/jquery-validation/jquery.metadata.js")"></script>
    <script src="@Url.Content("~/Content/roohui/jquery-validation/messages_cn.js")"></script>
    <style type="text/css">
        #layout1
        {
            width: 100%;
            margin: 0;
            padding: 0;
        }
    </style>
}
<div id="layout2">
    <div position="left" title="角色管理" id="rolemanage">
        <div id="toptoolbar">
        </div>
        <div id="divroleTree" style="overflow: auto" class="l-scroll">
            <ul id="roleTree" class="ztree">
            </ul>
        </div>
    </div>
    <div position="center" title="角色授权" id="rolemodule">
        <div id="authorizetoolbar">
        </div>
        <div id="divmoduletree" style="overflow: auto" class="l-scroll">
            <ul id="moduletree" class="ztree">
            </ul>
        </div>
        <input type="hidden" name="selectRoleID" id="selectRoleID" value="0" />
    </div>
</div>
<form action="/" method="post" id="frmRole" name="frmRole" style="background-color: white;
display: none">
<table cellpadding="0" cellspacing="0" class="l-table-edit">
    <tr>
        <td align="right" class="l-table-edit-td">
            <span class="l-star">*</span>&nbsp;角色名称:
        </td>
        <td align="left" class="l-table-edit-td">
            <input name="txtRoleName" type="text" id="txtRoleName" ltype="text" validate="{required:true}" />
        </td>
        <td align="left">
        </td>
    </tr>
    <tr>
        <td align="center" colspan="3">
            <span id="lblmsg" class="l-validate-error">&nbsp;</span>
            <input type="hidden" name="roleID" id="roleID" value="0" />
        </td>
    </tr>
</table>
</form>
<script type="text/javascript">
    var roleTree;
    var moduletree;
    var navtab = null;
    var savedialog;
    var layout;
    var rolemodule = null;
    var rolemanage = null;
    $(function () {
        layout = $("#layout2").ligerLayout({ leftWidth: 300, height: '100%',
            space: 4,
            onHeightChanged: f_heightChanged
        });

        $("#toptoolbar").ligerToolBar({ items: [{ text: '新增', click: tree_role_add, icon: "add" },
            { text: '修改', click: tree_role_edit, icon: "modify" },
            { text: '删除', click: tree_role_delete, icon: "delete" },
            { text: '刷新', click: role_refresh, icon: "refresh" }
            ]
        });

        $("#authorizetoolbar").ligerToolBar({ items: [
            { text: '授权', click: authorize, icon: "lock" },
            { text: '重置', click: reset_role_module, icon: "back" },
            { text: '刷新', click: refresh_role_module, icon: "refresh" }
            ]
        });


        roleTree = $("#roleTree").ligerTree({
            data: null,
            checkbox: false,
            slide: false,
            nodeWidth: 120,
            idFieldName: 'ID',
            parentIDFieldName: 'ParentID',
            textFieldName: 'Name',
            iconUrl: "/content/images/customers.gif",
            btnClickToToggleOnly: false,
            topParentIDValue: -1,
            attribute: ['ID'],
            onClick: function (node) {
                $("#selectRoleID").val(node.data.ID);
                layout.center.header.html('<div class="l-layout-header">给【' + node.data.Name + '】授权</div>');
                getRoleModule(node.data.ID);
            }
        });

        role_refresh();

        initModuleTree();


        $.metadata.setType("attr", "validate");
        var v = $("#frmRole").validate({
            //debug: true,
            errorPlacement: function (lable, element) {
                if (element.hasClass("l-textarea")) {
                    element.ligerTip({ content: lable.html(), target: element[0] });
                }
                else if (element.hasClass("l-text-field")) {
                    element.parent().ligerTip({ content: lable.html(), target: element[0] });
                }
                else {
                    lable.appendTo(element.parents("td:first").next("td"));
                }
            },
            success: function (lable) {
                lable.ligerHideTip();
                lable.remove();
            },
            submitHandler: function () {
                if ($('#txtRoleName').val() == "") {
                    $('#txtRoleName').focus();
                    $("#lblmsg").html('节点名称不能为空');
                    return;
                }
                $.ajax({
                    url: "/Role/RoleSave",
                    data: "roleName=" + $('#txtRoleName').val() + "&roleID=" + $('#roleID').val(),
                    type: "post",
                    success: function (result) {
                        //写提交ajax操作
                        if (result == "1") {
                            $.ligerDialog.success("保存成功。");
                            role_reset();
                            savedialog.hide();
                            role_refresh();
                        }
                        else {
                            $.ligerDialog.error("保存失败。");
                        }

                    }
                });
            }
        });

        $("#frmRole").ligerForm();

        
        $("#divroleTree").css("height", $(".l-layout-center").height() - 52);
        $("#divmoduletree").css("height", $(".l-layout-center").height() - 52);

    });
    function f_heightChanged(options) {
         
        $("#divroleTree").css("height", options.middleHeight - 52);
        $("#divmoduletree").css("height", options.middleHeight - 52);
    }


    function tree_role_add() {
        tree_role_show();
    }
    function save_role() {
        var form = $('#frmRole');
        form.submit();
    }

    function tree_role_edit() {
        if (roleTree.getSelected() == null) {
            $.ligerDialog.warn('请选择需要修改的角色！', '温馨提示');
            return;
        }
        var selected = roleTree.getSelected();
        tree_role_show(selected);
    }

    function tree_role_show(selected) {
        var title = "添加角色";
        if (selected != null) {
            title = "修改角色【 " + selected.data.Name + " 】"
            $("#txtRoleName").val(selected.data.Name);
            $("#roleID").val(selected.data.ID);
        } else {
            role_reset();
        }
        savedialog = $.ligerDialog.open({
            title: title,
            id: 'roleDialog',
            name: 'roleDialog',
            slide: false,
            isHidden: false,
            modal: true,
            target: $("#frmRole"),
            width: 300, isResize: false, allowClose: false,
            buttons: [
                { text: '保存', onclick: save_role },
                {
                    text: '取消', onclick: function (item, dialog) {
                        dialog.hide();
                        role_reset();
                    }
                }
            ]
        });
    }

    function tree_role_delete() {
        if (roleTree.getSelected() == null) {
            $.ligerDialog.warn('请选择需要删除的角色！', '温馨提示');
            return;
        }
        var selected = roleTree.getSelected();
        $.ligerDialog.confirm('删除后无法恢复，确定删除角色【' + selected.data.Name + '】吗？', '温馨提示', function (result) {
            if (result) {
                $.ajax({
                    url: '/Role/RoleDelete',
                    data: "roleID=" + selected.data.ID,
                    type: "post",
                    success: function (result) {
                        if (result == "1") {
                            //employee.deleteSelectedRow();
                            $.ligerDialog.success("成功删除角色【" + selected.data.Name + "】。");
                            role_refresh();
                        } else {
                            $.ligerDialog.error('删除失败！', '温馨提示');
                        }
                    }
                });
            }
        });
    }

    function role_refresh() {
        $.ajax({
            url: "/Role/GetList",
            data: null,
            type: 'post',
            success: function (result) {
                roleTree.setData(eval("(" + result + ")"));
            }
        });
        layout.center.header.html('<div class="l-layout-header">权限</div>');
        getRoleModule(0);
    }

    function role_reset() {
        $(".l-text-field").ligerHideTip();
        $("#txtRoleName").val("");
        $("#roleID").val("0");
        $("#lblmsg").html('');
    }
    function initModuleTree() {
        moduletree = $("#moduletree").ligerTree({
            data: null,
            checkbox: true,
            nodeWidth: 120,
            idFieldName: 'ID',
            parentIDFieldName: 'ParentID',
            textFieldName: 'Name',
            iconFieldName: 'Icon',
            btnClickToToggleOnly: false,
            topParentIDValue: -1,
            attribute: ['ID'],
            isReturnParent: true,
            onSelect: function (node) {

            }
        });
    }
    function getRoleModule(roleID) {

        $.ajax({
            url: "/Role/GetModuleByRoleID",
            data: 'roleID=' + roleID,
            type: 'Post',
            success: function (result) {
                moduletree.setData(eval("(" + result + ")"));
            }
        });
    }

    function refresh_role_module() {
        if (roleTree.getSelected() == null) {
            $.ligerDialog.warn('请选择需要刷新权限的角色！', '温馨提示');
            return;
        }
        var selected = roleTree.getSelected();
        getRoleModule(selected.data.ID);
    }
    function reset_role_module() {
        //var roleId = $("#selectRoleID").val();
        if (roleTree.getSelected() == null) {
            $.ligerDialog.warn('请选择需要重置权限的角色！', '温馨提示');
            return;
        }
        var selected = roleTree.getSelected();
        $.ligerDialog.confirm('重置将清空角色【' + selected.data.Name + '】的权限，是否继续？', '温馨提示', function (result) {
            if (result) {
                $.ajax({
                    url: "/Role/RoleAuthorizeDelete",
                    data: 'roleID=' + selected.data.ID,
                    type: 'Post',
                    success: function (result) {
                        if (result == "1") {
                            $.ligerDialog.success('重置成功，请及时授权！', '温馨提示');
                            getRoleModule(selected.data.ID);
                        }
                        else {
                            $.ligerDialog.warn('重置失败！', '温馨提示');
                        }
                    }
                });
            }
        });

    }


    function authorize() {
        if (roleTree.getSelected() == null) {
            $.ligerDialog.warn('请选择需要重置权限的角色！', '温馨提示');
            return;
        }
        var selected = roleTree.getSelected();
        var notes = moduletree.getChecked();
        var ids = "";
        if (notes.length == 0) {
            $.ligerDialog.warn('您还没有选择权限。', '温馨提示');
            return;
        }
        for (var i = 0; i < notes.length; i++) {
            ids += notes[i].data.ID + ",";
        }
        ids = ids.substring(0, ids.length - 1);
        var id = selected.data.ID;

        $.ajax({
            url: "/Role/RoleAuthorize",
            data: 'roleID=' + id + '&moduleIDs=' + ids,
            type: 'Post',
            success: function (result) {
                if (result == 1) {
                    $.ligerDialog.success('授权成功！', '温馨提示');
                    getRoleModule(selected.data.ID);
                }
                else {
                    $.ligerDialog.warn('授权失败！', '温馨提示');
                }
            }
        });

    }     
</script>
